﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Autocentro.Entities;
using Autocentro.BussinesLogic;

namespace Autocentro.UI
{
    public partial class FrmMttServicio : Form
    {
        [Dependency]
        public IProductoBL ServicioBL { get; set; }
        bool IsNew = true;
        
        public FrmMttServicio()
        {
            InitializeComponent();
            this.BuildUp();
        }
        public void AddServicio()
        {
            pRODUCTOBindingSource.AddNew();
            

        }
        public PRODUCTO _servicio { get; set; }
        public void UpdateServicio()
        {
            IsNew = false;
            pRODUCTOBindingSource.DataSource = _servicio;
            
            
        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
              
            var servicio = pRODUCTOBindingSource.Current as PRODUCTO;
            var msg = "ERROR";

            if (servicio!=null)
            {
                servicio.Descripcion = descripcionTextBox.Text.ToUpper();
                servicio.ProductoServicio = true;
                servicio.IdUnidad = 1;

                if (IsNew)
                {
                    try
                    {
                        ServicioBL.AddProducto(servicio);
                        MessageBox.Show("Registro Correcto", "Mensaje",MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje",MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    try
                    {
                        ServicioBL.UpdateProducto(servicio);
                        UpdateServicio();
                        MessageBox.Show("Modificación Correcta", "Mensaje",MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string cadena = Convert.ToString("-") + Convert.ToString("/") + Convert.ToString(".") + Convert.ToString(",") + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar) || (char.IsNumber(e.KeyChar)))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void costoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string cadena = Convert.ToString(".") + (char)8 + Convert.ToString(",");

            if (Char.IsDigit(e.KeyChar) || cadena.Contains(e.KeyChar) )
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void FrmMttServicio_Load(object sender, EventArgs e)
        {

        }

    }
}
